package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public R add(ShoppingCart shoppingCart) {
        //补全参数 获取当前登录用户,给购物车对象赋值
        shoppingCart.setUserId(BaseContext.threadLocal.get());
        //针对套餐个菜品分开操作
        if (shoppingCart.getDishId() != null) {//菜品
            //查询购物车
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            //添加条件
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            wrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
            //口味条件
            if (shoppingCart.getDishFlavor() != null) {
                wrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
            }
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
            System.out.println(shoppingCarts.toString());
            insertOrUpdate(shoppingCarts, shoppingCart);
        } else {//套餐
            //查询购物车
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            //添加条件
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            wrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
            insertOrUpdate(shoppingCarts, shoppingCart);
        }
        //todo 添加或修改成功后,把修改或者添加的对象返回到前端
        if (shoppingCart.getDishId() != null && shoppingCart.getDishFlavor() != null) {
            shoppingCart.setNumber(null);
            return R.success(shoppingCart);
        } else {
            return R.success(shoppingCart);
        }
    }

    @Override
    public R findById() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.threadLocal.get());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);
        return R.success(shoppingCarts);
    }

    @Override
    public void cleanById() {
        Long userId = BaseContext.threadLocal.get();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartMapper.delete(wrapper);
    }

    @Override
    public R sub(ShoppingCart shoppingCart) {
        int num=-1;
        //拿到用户id
        Long userId = BaseContext.threadLocal.get();
        //判断是菜品还是套餐
        if (shoppingCart.getSetmealId() != null) {//套餐
            //先从数据库拿到具体数据
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            //用户id
            wrapper.eq(ShoppingCart::getUserId, userId);
            //套餐id
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(wrapper);

            if (shoppingCart1.getNumber() == 1) {
                //只有一份直接删除
                shoppingCartMapper.delete(wrapper);
            } else {
                //多份,数量-1
                shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
                num=shoppingCart1.getNumber();
                shoppingCartMapper.updateById(shoppingCart1);
            }
        }

        if (shoppingCart.getDishId() != null) {//菜品
            //先从数据库拿到具体数据
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            //用户id
            wrapper.eq(ShoppingCart::getUserId, userId);
            //菜品id
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            //菜品口味
            if (shoppingCart.getDishFlavor() != null) {
                wrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
            }
            ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(wrapper);
            if (shoppingCart1.getNumber() == 1) {
                //只有一份直接删除
                shoppingCartMapper.delete(wrapper);
            } else {
                //多份,数量-1
                shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
                num=shoppingCart1.getNumber();
                shoppingCartMapper.updateById(shoppingCart1);
            }
        }
        //返回数据
        shoppingCart.setNumber(num);
        return R.success(shoppingCart);
    }

    private void insertOrUpdate(List<ShoppingCart> shoppingCarts, ShoppingCart shoppingCart) {
        if (shoppingCarts.size() == 0) {//购物车里没有
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        } else {
            ShoppingCart shoppingCartDB = shoppingCarts.get(0);
            shoppingCartDB.setNumber(shoppingCartDB.getNumber() + 1);
            //存储到数据库中
            shoppingCartMapper.updateById(shoppingCartDB);
            //封装返回数据
            shoppingCart.setNumber(shoppingCartDB.getNumber());
            shoppingCart.setAmount(shoppingCartDB.getAmount());
            shoppingCart.setDishFlavor(shoppingCartDB.getDishFlavor());
            shoppingCart.setDishId(shoppingCartDB.getDishId());
            shoppingCart.setSetmealId(shoppingCartDB.getSetmealId());
            shoppingCart.setImage(shoppingCartDB.getImage());
        }
    }
}
